package com.farm.area.service.impl;

import com.farm.area.dao.FarmLandDeviceMapper;
import com.farm.area.dao.FarmLandMapper;
import com.farm.area.pojo.FarmLand;
import com.farm.area.pojo.TCustFarmlandDevice;
import com.farm.area.service.FarmLandDeviceService;
import com.farm.device.pojo.Device;
import com.farm.opjo.Crops;
import com.farm.shop.pojo.CustDevice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 陈哲
 * @create 2020/10/16 14:23
 */
@Service
@Transactional
public class FarmLandDeviceServiceImpl implements FarmLandDeviceService {

    @Autowired
    private FarmLandDeviceMapper farmLandDeviceMapper;
    @Autowired
    private FarmLandMapper farmLandMapper;

    /**
     * 大田基本信息
     * @param farmlandId
     * @return
     */
    @Override
    public FarmLand basicInformationOfFarm(Integer farmlandId) {
        FarmLand farmLand = new FarmLand();
        //查询大田的基本信息
        FarmLand f = farmLandMapper.selectByPrimaryKey(farmlandId);
        farmLand.setId(f.getId());
        farmLand.setFarmId(f.getFarmId());
        farmLand.setSize(f.getSize());
        farmLand.setShape(f.getShape());
        farmLand.setCode(f.getCode());
        farmLand.setStatus(f.getStatus());
        //查询大田的农作物
        Crops crops = farmLandDeviceMapper.findCropsByFarmLandId(farmlandId);
        farmLand.setCrops(crops);
        //查询大田所有的设备
        List<CustDevice> custDevices = farmLandDeviceMapper.findDevicesByFarmLandId(farmlandId);
       /* List<Device> devices = new ArrayList<>();
        for (CustDevice custDevice : custDevices) {
            Integer did = custDevice.getDid();
            Device device = farmLandDeviceMapper.findBydid(did);
            devices.add(device);
        }*/
        farmLand.setDevices(custDevices);
        return farmLand;
    }

    /**
     * 关闭设备
     * @param cdid
     * @return
     */
    @Override
    public int close(Integer cdid) {
        TCustFarmlandDevice farmlandDevice = farmLandDeviceMapper.selectByCdid(cdid);
        farmlandDevice.setStatus("0");
        int i = farmLandDeviceMapper.updateStatusByFarmLandDevice(farmlandDevice);
        return i;
    }

    /**
     * 开启设备
     * @param cdid
     * @return
     */
    @Override
    public int start(Integer cdid) {
        TCustFarmlandDevice farmlandDevice = farmLandDeviceMapper.selectByCdid(cdid);
        farmlandDevice.setStatus("1");
        int i = farmLandDeviceMapper.updateStatusByFarmLandDevice(farmlandDevice);
        return i;
    }
}
